package com.dqhl.business.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dqhl.business.R;
import com.dqhl.business.modle.Promotion;
import com.dqhl.business.util.Config;
import com.dqhl.business.util.ViewHolder;

import org.xutils.x;

import java.util.List;

/**
 * Created by guanluocang on 2016/11/8.
 */
public class PromotionReleaseAdapter extends BaseAdapter {
    private List<Promotion> promotionList;
    private Context context;
    private LayoutInflater inflater;
    private OnReleaserListener mOnReleaserListener;

    public PromotionReleaseAdapter(Context context,List<Promotion> promotionList,OnReleaserListener mOnReleaserListener){
        this.context = context;
        this.promotionList = promotionList;
        this.mOnReleaserListener = mOnReleaserListener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return promotionList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if(view == null){
            view = inflater.inflate(R.layout.layout_promotion_item,viewGroup,false);
        }
        TextView tv_campaignNum = ViewHolder.get(view,R.id.tv_campaignNum);
        TextView tv_campaignName = ViewHolder.get(view,R.id.tv_campaignName);;
        TextView tv_campaignGoods = ViewHolder.get(view,R.id.tv_campaignGoods);;
        TextView tv_campaignTime = ViewHolder.get(view,R.id.tv_campaignTime);;
        TextView tv_campaignPromotion = ViewHolder.get(view,R.id.tv_campaignPromotion);
        ImageView iv_campaignIcon = ViewHolder.get(view,R.id.iv_campaignIcon);
        TextView tv_release = ViewHolder.get(view,R.id.tv_release);
        Promotion promotion = promotionList.get(i);
        tv_campaignNum.setText(promotion.getCode());
        tv_campaignName.setText(promotion.getName());
        tv_campaignGoods.setText(promotion.getGoods());
        tv_campaignTime.setText(promotion.getTime());
        tv_campaignPromotion.setText(promotion.getCoupon_id());
        x.image().bind(iv_campaignIcon, Config.img_url+promotion.getGoods_pic());

        if("1".equals(promotion.getState())){
            tv_release.setVisibility(View.VISIBLE);
        }else{
            tv_release.setVisibility(View.GONE);
        }
        tv_release.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnReleaserListener.onReleaser(i);
            }
        });
        return view;
    }

    public interface OnReleaserListener{
        void onReleaser(int position);
    }
}
